<!-- pages/community/index.vue -->
<template>
  <view class="container">
    <!-- 顶部导航栏 -->
    <view class="nav-bar" style="background: linear-gradient(135deg, #A8BF8F 0%, #89A172 100%);">
      <text class="nav-title">考研社区 💭</text>
    </view>

    <!-- 内容区域 -->
    <scroll-view scroll-y class="content">
      <!-- 热门帖子模块 -->
      <view class="section">
        <view class="section-header">
          <view class="title-wrapper">
            <view class="title-dot"></view>
            <text class="section-title">热门帖子 🔥</text>
          </view>
          <text class="more">更多 ></text>
        </view>
        <view class="post-list">
          <view class="post-item" 
                v-for="(post, index) in hotPosts" 
                :key="index"
                @click="navigateToDetail(post.id)">
            <image :src="post.avatar" class="avatar" mode="aspectFill"></image>
            <view class="post-content">
              <text class="username">{{post.username}} {{index === 0 ? '👑' : ''}}</text>
              <text class="title">{{post.title}}</text>
              <view class="post-stats">
                <view class="stat-item">
                  <text class="iconfont icon-like">👍</text>
                  <text>{{post.likes}}</text>
                </view>
                <view class="stat-item">
                  <text class="iconfont icon-comment">💬</text>
                  <text>{{post.comments}}</text>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>

      <!-- 搭子推荐模块 -->
      <view class="section" @click="navigateToDetail(id)">
        <view class="section-header">
          <view class="title-wrapper">
            <view class="title-dot"></view>
            <text class="section-title">搭子推荐 🤝</text>
          </view>
          <text class="more">更多 ></text>
        </view>
        <scroll-view scroll-x class="partner-list">
          <view class="partner-item" v-for="(partner, index) in partners" :key="index">
            <view class="partner-card">
              <image :src="partner.avatar" class="partner-avatar" mode="aspectFill"></image>
              <text class="partner-name">{{partner.name}} ✨</text>
              <text class="partner-major">{{partner.major}} 📚</text>
              <button class="connect-btn" 
                      style="background: linear-gradient(135deg, #A8BF8F 0%, #89A172 100%);" 
                      @click.stop="navigateToPartner(partner.id)">加为搭子 🤗</button>
            </view>
          </view>
        </scroll-view>
      </view>

      <!-- 打卡动态模块 -->
      <view class="section">
        <view class="section-header">
          <view class="title-wrapper">
            <view class="title-dot"></view>
            <text class="section-title">打卡动态 ✅</text>
          </view>
          <text class="more">更多 ></text>
        </view>
        <view class="checkin-list">
          <view class="checkin-item" v-for="(checkin, index) in checkins" :key="index">
            <image :src="checkin.avatar" class="avatar" mode="aspectFill"></image>
            <view class="checkin-content">
              <text class="username">{{checkin.username}} {{index === 0 ? '🌟' : ''}}</text>
              <text class="checkin-text">{{checkin.content}}</text>
              <text class="checkin-time">{{checkin.time}} ⏰</text>
            </view>
          </view>
        </view>
      </view>
    </scroll-view>

    <!-- 悬浮发帖按钮 -->
    <view class="float-btn" @click="navigateToPost">
      <text class="plus">✏️</text>
    </view>

    <!-- 底部导航栏 -->
    <view class="bottom-tab-bar">
      <view class="tab-item" 
            v-for="(item, index) in bottomNavItems" 
            :key="index"
            @tap="switchTab(index)">
        <image :src="currentTab === index ? item.activeIcon : item.icon" 
               mode="aspectFit" 
               class="tab-icon"></image>
        <text class="tab-text"
              :class="{ 'active': currentTab === index }">{{ item.text }}</text>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue'

// 当前选中的tab（社区页是第三个tab，索引为2）
const currentTab = ref(2)

// 底部导航数据
const bottomNavItems = ref([
  { 
    icon: '/static/icons/politics.png',
    activeIcon: '/static/icons/politics.png', 
    text: '首页',
    url: '/pages/index/index'
  },
  { 
    icon: '/static/icons/练习.png',
    activeIcon: '/static/icons/练习.png', 
    text: '练习',
    url: '/pages/practice/practice'
  },
  { 
    icon: '/static/icons/home1.png',
    activeIcon: '/static/icons/home1.png', 
    text: '社区',
    url: '/pages/community/index'
  },
  { 
    icon: '/static/icons/user.png',
    activeIcon: '/static/icons/user-active.png', 
    text: '我的',
    url: '/pages/my/my'
  }
])

// 帖子数据
const hotPosts = ref([
  {
    id: 1,
    avatar: '/static/my/男孩.png',
    username: '考研人加油',
    title: '数学一高数知识点总结分享',
    likes: 128,
    comments: 32
  },
  {
    id: 2,
    avatar: '/static/my/头像 女孩.png',
    username: '英语达人',
    title: '考研英语阅读理解答题技巧',
    likes: 89,
    comments: 25
  },
  {
    id: 3,
    avatar: '/static/my/girl-9.png',
    username: '政治小王子',
    title: '肖秀荣政治冲刺背诵笔记',
    likes: 76,
    comments: 18
  }
])

// 搭子数据
const partners = ref([
  {
    id: 1,
    avatar: '/static/my/男孩.png',
    name: '小明',
    major: '计算机科学'
  },
  {
    id: 2,
    avatar: '/static/my/girl-9.png',
    name: '小红',
    major: '应用数学'
  },
  {
    id: 3,
    avatar: '/static/my/男孩.png',
    name: '小华',
    major: '软件工程'
  }
])

// 打卡数据
const checkins = ref([
  {
    avatar: '/static/my/柯南.png',
    username: '奋斗小子',
    content: '今日完成高数五套题，继续努力！',
    time: '10分钟前'
  },
  {
    avatar: '/static/my/柯南.png',
    username: '考研加油',
    content: '英语阅读打卡第30天，坚持就是胜利',
    time: '1小时前'
  },
  {
    avatar: '/static/my/柯南.png',
    username: '学霸妹妹',
    content: '政治1000题刷完啦，心得分享...',
    time: '2小时前'
  }
])

// 页面跳转方法
const navigateToDetail = (id) => {
  uni.navigateTo({
    url: `/pages/community/detail`
  })
}

const navigateToPartner = (id) => {
  uni.navigateTo({
    url: '/pages/partner/request-sent'
  })
}

const navigateToPost = () => {
  uni.navigateTo({
    url: '/pages/community/post'
  })
}

// 底部标签页切换方法
const switchTab = (index) => {
  const item = bottomNavItems.value[index]
  currentTab.value = index
  
  uni.switchTab({
    url: item.url,
    success: () => {
      console.log('页面跳转成功')
    },
    fail: (err) => {
      console.error('切换标签页失败：', err)
      uni.navigateTo({
        url: item.url,
        fail: (navigateErr) => {
          console.error('导航失败：', navigateErr)
          uni.showToast({
            title: '页面跳转失败',
            icon: 'none'
          })
        }
      })
    }
  })
}
</script>

<style>
.container {
  flex: 1;
  background-color: #f8f9fa;
  padding-bottom: 120rpx;
}

.nav-bar {
  height: 88rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 30rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
}

.nav-title {
  color: #fff;
  font-size: 36rpx;
  font-weight: bold;
  letter-spacing: 2rpx;
}

.content {
  flex: 1;
  padding: 20rpx;
}

.section {
  background-color: #fff;
  border-radius: 16rpx;
  padding: 24rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
}

.title-wrapper {
  display: flex;
  align-items: center;
}

.title-dot {
  width: 8rpx;
  height: 32rpx;
  background-color: #A8BF8F;
  border-radius: 4rpx;
  margin-right: 16rpx;
}

.section-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.more {
  color: #999;
  font-size: 28rpx;
}

.post-item {
  display: flex;
  padding: 24rpx;
  margin-bottom: 16rpx;
  background: #fff;
  border-radius: 12rpx;
  border: 1rpx solid #f0f0f0;
  transition: all 0.3s ease;
}

.post-item:active {
  background-color: #f8f9fa;
}

.avatar {
  width: 80rpx;
  height: 80rpx;
  border-radius: 40rpx;
  margin-right: 20rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
}

.post-content {
  flex: 1;
}

.username {
  font-size: 28rpx;
  color: #666;
  margin-bottom: 8rpx;
}

.title {
  font-size: 30rpx;
  color: #333;
  margin: 10rpx 0;
  font-weight: 500;
}

.post-stats {
  display: flex;
  gap: 24rpx;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 6rpx;
  color: #999;
  font-size: 24rpx;
}

.partner-list {
  white-space: nowrap;
  padding: 10rpx 0;
}

.partner-item {
  display: inline-block;
  width: 220rpx;
  margin-right: 20rpx;
}

.partner-card {
  background: #fff;
  padding: 20rpx;
  border-radius: 12rpx;
  text-align: center;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.partner-card:active {
  transform: scale(0.98);
}

.partner-avatar {
  width: 120rpx;
  height: 120rpx;
  border-radius: 60rpx;
  margin-bottom: 16rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
}

.partner-name {
  font-size: 28rpx;
  color: #333;
  font-weight: 500;
  margin-bottom: 8rpx;
}

.partner-major {
  font-size: 24rpx;
  color: #666;
  margin-bottom: 16rpx;
}

.connect-btn {
  font-size: 24rpx;
  color: #fff;
  padding: 8rpx 24rpx;
  border-radius: 30rpx;
  border: none;
}

.checkin-item {
  display: flex;
  padding: 24rpx;
  margin-bottom: 16rpx;
  background: #fff;
  border-radius: 12rpx;
  border: 1rpx solid #f0f0f0;
  transition: all 0.3s ease;
}

.checkin-item:active {
  background-color: #f8f9fa;
}

.checkin-content {
  flex: 1;
}

.checkin-text {
  font-size: 28rpx;
  color: #333;
  margin: 10rpx 0;
  line-height: 1.5;
}

.checkin-time {
  font-size: 24rpx;
  color: #999;
}

.float-btn {
  position: fixed;
  right: 40rpx;
  bottom: 160rpx;
  width: 100rpx;
  height: 100rpx;
  background: linear-gradient(135deg, #A8BF8F 0%, #89A172 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4rpx 16rpx rgba(168, 191, 143, 0.3);
  transition: transform 0.2s;
  z-index: 99;
}

.float-btn:active {
  transform: scale(0.95);
}

.plus {
  color: #fff;
  font-size: 60rpx;
}

/* 底部导航栏样式 */
.bottom-tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100rpx;
  background-color: #ffffff;
  display: flex;
  justify-content: space-around;
  align-items: center;
  border-top: 1rpx solid #eeeeee;
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 999;
  box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.tab-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 10rpx 0;
  position: relative;
}

.tab-item:active {
  opacity: 0.8;
}

.tab-icon {
  width: 48rpx;
  height: 48rpx;
  margin-bottom: 6rpx;
}

.tab-text {
  font-size: 24rpx;
  color: #666666;
  line-height: 1.2;
}

.tab-text.active {
  color: #A8BF8F;
  font-weight: bold;
}

/* 适配iPhone底部安全区 */
@supports (bottom: constant(safe-area-inset-bottom)) or (bottom: env(safe-area-inset-bottom)) {
  .bottom-tab-bar {
    padding-bottom: constant(safe-area-inset-bottom);
    padding-bottom: env(safe-area-inset-bottom);
  }
}

/* 暗黑模式适配 */
@media (prefers-color-scheme: dark) {
  .container {
    background-color: #000000;
  }

  .section {
    background-color: #1c1c1e;
  }

  .post-item,
  .partner-card,
  .checkin-item {
    background-color: #1c1c1e;
    border-color: #2c2c2e;
  }

  .section-title,
  .title,
  .partner-name {
    color: #ffffff;
  }

  .username,
  .partner-major,
  .checkin-text {
    color: #8e8e93;
  }

  .more,
  .checkin-time {
    color: #6e6e73;
  }

  .bottom-tab-bar {
    background-color: #1c1c1e;
    border-top-color: #2c2c2e;
  }
  
  .tab-text {
    color: #8e8e93;
  }
  
  .tab-text.active {
    color: #A8BF8F;
  }

  .exercise-item {
    border-bottom-color: #2c2c2e;
  }

  .exercise-item:active {
    background-color: #2c2c2e;
  }
}
</style>